package edu.tyut.game.ui.user

import android.os.CountDownTimer
import android.widget.Button
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
import edu.tyut.game.R

class SharedViewModel: ViewModel() {
    // 60s 读秒
    private val secondLiveData = MutableLiveData<Long>()
    val second = secondLiveData.map {
        it
    }
    fun countDown(seconds: Long, button : Button){
        // TODO 旋转屏幕时有bug
        button.isEnabled = false
        var second = seconds / 1000
        val countDownTimer = object : CountDownTimer(seconds, 1000){
            override fun onTick(millisUntilFinished: Long) {
                secondLiveData.value = -- second

            }
            override fun onFinish() {
                button.isEnabled = true
                button.setText(R.string.verification_code)
            }
        }
        countDownTimer.start()
        secondLiveData.value = second
    }
}